package com.nstar.base.util
{
	import com.adobe.images.PNGEncoder;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.StatusEvent;
	import flash.geom.Matrix;
	import flash.media.Camera;
	import flash.media.Video;
	
	import mx.controls.VideoDisplay;
	import mx.utils.Base64Encoder;
	
	public class CameraUtils
	{
		private static var inst:CameraUtils;
		
		private var vid:Video = new Video();
		private var cam:Camera ;
		private var vdCamera:VideoDisplay;
		
		//默认摄像头图像大小为320X240
		private static const DEFAULT_WIDTH:int = 320;
		private static const DEFAULT_HEIGHT:int = 240;
		
		//是否使用默认摄像头大小
		public var useDefaultSize:Boolean = true;
		
		public function CameraUtils()
		{
			//TODO: implement function
		}
		
		//单例模式，参数为是否使用默认图像大小，缺省指定默认大小为320X240
		public static function getInstance(useDefaultSize:Boolean=true):CameraUtils{
			if(inst==null){
				inst = new CameraUtils();
			}
			inst.useDefaultSize = useDefaultSize;
			return inst;
		}
		
		
		//获取摄像头，并将图像显示在vdCamera里
		public function getCamera(vdCamera:VideoDisplay):Camera{
			cam = Camera.getCamera();
			if(cam==null){
				return null;
			}
			this.vdCamera = vdCamera;
			cam.addEventListener(StatusEvent.STATUS,statusHandler);
			vid.attachCamera(cam);
			vdCamera.addChild(vid);
			return cam;
		}	
		
		//状态监控
		private function statusHandler(event:StatusEvent):void{
			switch(event.code){
				case "Camera.Muted":
					break;
				case "Camera.Unmuted":
					if(!useDefaultSize){
				        vid.width = cam.width;
				        vid.height = cam.height;	
					}else{
						vid.width = DEFAULT_WIDTH;
						vid.height = DEFAULT_HEIGHT;
					}
					break;
			}
			cam.removeEventListener(StatusEvent.STATUS,statusHandler);//删除事件监控
		}
		
		//获取摄像头图像的位图,可通过Image的addChild(bitmap)来设置图像来源
		public function getBitmap():Bitmap{
			var m_pictureBitmapData:BitmapData; //定义视频截图
		    m_pictureBitmapData = new BitmapData(vid.width,vid.height);
		    m_pictureBitmapData.draw(vdCamera,new Matrix());
		    var m_pictureBitmap:Bitmap = new Bitmap(m_pictureBitmapData);	
		    return 	m_pictureBitmap;
		}
		
		//获取摄像头图像宽度
		public function getCameraWidth():Number{
			var size:Number = 0;
			if (cam!=null){
				size = cam.width;
			}
			return size;
		}
		
		//获取摄像头图像高度
		public function getCameraHeight():Number{
			var size:Number = 0;
			if (cam!=null){
				size = cam.height;
			}
			return size;
		}
		
		//获取组件的快照
		public function takeSnapshot(comp:DisplayObject):String {
			var bitmapData:BitmapData = new BitmapData(comp.width, comp.height, true, 0xffffff);
			bitmapData.draw(comp);
			
			var bytes:ByteArray = PNGEncoder.encode(bitmapData);
		
			var b64encoder:Base64Encoder = new Base64Encoder();
			b64encoder.encodeBytes(bytes);
			//ExternalInterface.call("takeSnapshot", b64encoder.flush(), comp.width + 10, comp.height + 10);
			return b64encoder.flush();
		} 
	}
}